// pages/petDetails/petDetails.js

var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,

    petTypeId:212,
    petInformation:'',
    aaa:"",
    bbb:"",
    ddd:"",
    falg:true,

    ycq: "",
    ycb: "",
    yc: "",
    ycfalg:true,
    yccharacter: "../../utils/img/xh@2x.png",
    character:"../../utils/img/xh@2x.png",
  },
  aaa:function(){
    if(!that.data.falg){
      
      that.setData({
        falg: !that.data.falg,
       character: "../../utils/img/xh@2x.png",
        ddd:that.data.bbb
      })
      
    }else{
      that.setData({
        falg: !that.data.falg,
        character: "../../utils/img/sh@2x.png",
       ddd: that.data.aaa
      })
    }
  },
  ycs:function(){

    if (!that.data.ycfalg) {
      
      that.setData({
        ycfalg: !that.data.ycfalg,
        yccharacter: "../../utils/img/xh@2x.png",
        yc: that.data.ycb
      })

    } else {
      that.setData({
        ycfalg: !that.data.ycfalg,
        yccharacter: "../../utils/img/sh@2x.png",
        yc: that.data.ycq
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.setData({
      petTypeId: options.petTypeId
    })
    that.getPetTypeById();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**   
     * 预览图片  
     */
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.imgUrls // 需要预览的图片http链接列表  
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getPetTypeById:function(){
    
    wx.request({
      url: getApp().globalData.urls + '/wxgz/petType/getPetTypeById',
      data: {
        petTypeId: that.data.petTypeId
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        //console.log(res.data)
        var code = res.data.code
        if (code == 200) {
          that.setData({
            petInformation:res.data.data,
            imgUrls: res.data.data.imageUrls,
            aaa: that.convertHtmlToText(res.data.data.characterContent),
            bbb: that.convertHtmlToText(res.data.data.characterContent).substring(0, 100),
            ddd: that.convertHtmlToText(res.data.data.characterContent).substring(0, 100),

            ycq: that.convertHtmlToText(res.data.data.environment),
            ycb: that.convertHtmlToText(res.data.data.environment).substring(0, 100),
            yc: that.convertHtmlToText(res.data.data.environment).substring(0, 100)
          }) 
        } 
        if (that.data.aaa.length > 100) {
          that.data.bbb = that.data.bbb + "......"
        }
        
        if (that.data.ycq.length > 100) {
          that.data.ycb = that.data.ycb + "......"
        }
      }
    })
  },
  convertHtmlToText: function convertHtmlToText(inputText) {
    var returnText = "" + inputText; 
    returnText = returnText.replace(/<\/div>/ig, '\r\n'); 
    returnText = returnText.replace(/<\/li>/ig, '\r\n'); 
    returnText = returnText.replace(/<li>/ig, ' * '); 
    returnText = returnText.replace(/<\/ul>/ig, '\r\n');
    //-- remove BR tags and replace them with line break
    returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");
    //-- remove P and A tags but preserve what's inside of them
    returnText = returnText.replace(/<p.*?>/gi, "\r\n");

    returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");
    //-- remove all inside SCRIPT and STYLE tags
    returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText = returnText.replace(/<(?:.|\s)*?>/g, "");
    //-- get rid of more than 2 multiple line breaks:
    returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");
    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g, '');
    //-- get rid of html-encoded characters:
    returnText = returnText.replace(/ /gi, " ");
    returnText = returnText.replace(/&/gi, "&");
    returnText = returnText.replace(/"/gi, '"');
    returnText = returnText.replace(/</gi, '<');
    returnText = returnText.replace(/>/gi, '>');

 
    return returnText.trim();

  }

})